package com.ruoyi.project.WxMoudle.api;


import com.github.binarywang.wxpay.util.SignUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.interceptor.annotation.IgnoreAuth;
import com.ruoyi.project.WebMoudle.WxCitys.domain.WxCitys;
import com.ruoyi.project.WebMoudle.WxCitys.service.IWxCitysService;
import com.ruoyi.project.WebMoudle.WxShop.domain.WxShop;
import com.ruoyi.project.WebMoudle.WxShopType.domain.WxShopType;
import com.ruoyi.project.WebMoudle.WxShopType.service.IWxShopTypeService;
import com.ruoyi.project.WebMoudle.WxShopWords.domain.WxShopWords;
import com.ruoyi.project.WebMoudle.WxShopWords.service.IWxShopWordsService;
import com.ruoyi.project.WebMoudle.advShow.domain.AdvShow;
import com.ruoyi.project.WebMoudle.advertSeat.domain.AdvertSeat;
import com.ruoyi.project.WebMoudle.carousel.domain.Carousel;
import com.ruoyi.project.WebMoudle.carousel.service.ICarouselService;
import com.ruoyi.project.WebMoudle.carouselSeat.domain.CarouselSeat;
import com.ruoyi.project.WebMoudle.carouselSeat.service.ICarouselSeatService;
import com.ruoyi.project.WebMoudle.useTerms.domain.UseTerms;
import com.ruoyi.project.WebMoudle.useTerms.service.IUseTermsService;
import com.ruoyi.project.WebMoudle.wxHelp.domain.WxHelp;
import com.ruoyi.project.WebMoudle.wxHelp.service.IWxHelpService;
import com.ruoyi.project.WxMoudle.Bases.AppController;
import com.ruoyi.project.WxMoudle.Bases.WeixinConfig;
import com.ruoyi.project.WxMoudle.WebSocket.WebSocketServer;
import jdk.nashorn.internal.ir.annotations.Ignore;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/wx/data")
public class WxDataController extends AppController {

    @Autowired
    private ICarouselService carouselService;

    @Autowired
    private IWxShopTypeService wxShopTypeService;

    @Autowired
    private IWxCitysService citysService;

    @Autowired
    private ICarouselSeatService carouselSeatService;

    @Autowired
    private WxService wxService;
    @Autowired
    private IWxShopWordsService wordsService;
    @Autowired
    private IUseTermsService termsService;
    @Autowired
    private IWxHelpService wxHelpService;
    @Autowired
    private WebSocketServer webSocketServer;


    /*查询首页轮播*/
    @RequestMapping(value = "/queryNowList")
    @IgnoreAuth
    @ResponseBody
    public Object queryNowList() {
        List<Carousel> list = carouselService.selectCarouselList(null);
        return list;
    }

    /*查询商铺类型*/
    @RequestMapping(value = "/selectShopTypeList")
    @IgnoreAuth
    @ResponseBody
    public Object selectShopTypeList() {
        WxShopType wxShopType = new WxShopType();
        wxShopType.setIsOpen("0");
        List<WxShopType> list = wxShopTypeService.selectWxShopTypeList(wxShopType);
        return list;
    }

    /*查询城市列表*/
    @RequestMapping(value = "/selectCityList")
    @IgnoreAuth
    @ResponseBody
    public Object selectCityList() {
        List<WxCitys> list = citysService.selectLastList();
        return list;
    }

    /*根据时间查询可以使用的轮播位*/
    @RequestMapping(value = "/selectSeatList")
    @ResponseBody
    @IgnoreAuth
    public Object selectSeatList(String startTime, String endTime) {
        List<CarouselSeat> list = carouselSeatService.queryUsableSeat(startTime, endTime);
        return list;
    }

    /*申请使用广告位*/
    @RequestMapping(value = "/appplySeat")
    @ResponseBody
    @IgnoreAuth
    public Object appplySeat(MultipartFile[] imgs, Carousel carousel) throws IOException {
        if (imgs != null) {
            for (MultipartFile file : imgs) {
                //上传图片
                String filePath2 = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String imgUrl = FileUploadUtils.upload(filePath2, file);
                if (StringUtils.isNotEmpty(carousel.getImgUrl())) {
                    carousel.setImgUrl(carousel.getImgUrl() + "," + imgUrl);
                } else {
                    carousel.setImgUrl(imgUrl);
                }
            }
        } else {
            //没有营业执照
            return toResponsFail("缺少图片");
        }
        carouselService.addCarousel(carousel);
        return writeResultRep();
    }

    /*根据店铺id查询申请列表*/
    @RequestMapping(value = "/querySeatListByShopId")
    @ResponseBody
    @IgnoreAuth
    public Object querySeatListByShopId(Long shopId) {
        Carousel carousel = new Carousel();
        carousel.setShopId(shopId);
        List<Carousel> advShows = carouselService.selectCarouselList(carousel);
        return advShows;
    }

    /**
     * 签名信息
     *
     * @param currurl
     * @return
     */
    @RequestMapping("/getWeixinPram")
    @IgnoreAuth
    @ResponseBody
    public Object getWeixinPram(String currurl) {
        try {
            Date date = new Date();
            Map<String, String> params = new HashMap<String, String>();
            params.put("appId", WeixinConfig.APPID);
            params.put("timeStamp", date.getTime() + "");
            params.put("nonceStr", (int) ((Math.random() * 9 + 1) * 100000) + "");//6位随机数
            params.put("signType", "MD5");//加密方式
            //params.put("package", "prepay_id=" + result.getPrepayId());
            String sign = SignUtils.createSign(params, "MD5", WeixinConfig.PAYKEY, true);
            params.put("sign", sign);

            WxJsapiSignature wxJsapiSignature = wxService.createJsapiSignature(currurl);
            params.put("wxsign", wxJsapiSignature.getSignature());
            params.put("wxnonceStr", wxJsapiSignature.getNonceStr());
            params.put("wxtimeStamp", wxJsapiSignature.getTimestamp() + "");

            return writeResultRep(params);

        } catch (Exception e) {
            e.printStackTrace();
            return writeResultFailure(e.getMessage());
        }

    }

    /*根据店铺id查询申请列表*/
    @RequestMapping(value = "/selectWordsList")
    @ResponseBody
    @IgnoreAuth
    public Object selectWordsList() {
        WxShopWords words = new WxShopWords();
        words.setIsValid("0");
        List<WxShopWords> wordsList = wordsService.selectWxShopWordsList(words);
        return wordsList;
    }

    /*查询使用条款*/
    @RequestMapping(value = "/queryContent")
    @ResponseBody
    @IgnoreAuth
    public Object queryContent() {
        List<UseTerms> list = termsService.selectUseTermsList(null);
        String content = list.get(0).getContent();
        return content;
    }

    /*查询广告条款*/
    @RequestMapping(value = "/queryAdvContent")
    @ResponseBody
    @IgnoreAuth
    public Object queryAdvContent() {
        List<UseTerms> list = termsService.selectUseTermsList(null);
        String advContent = list.get(0).getAdvContent();
        return advContent;
    }

    /*展示常见问题*/
    @RequestMapping(value = "/showSysQuestion")
    @ResponseBody
    @IgnoreAuth
    public Object showSysQuestion() {
        WxHelp wxHelp = new WxHelp();
        wxHelp.setQuestionType("0");
        List<WxHelp> wxHelps = wxHelpService.selectWxHelpList(wxHelp);
        return wxHelps;
    }

    /*查询某一系统问题*/
    @RequestMapping(value = "/queryOneHelp")
    @ResponseBody
    @IgnoreAuth
    public Object queryOneHelp(Long id) {

        WxHelp wxHelp = wxHelpService.selectWxHelpById(id);
        return wxHelp;
    }

    /*添加用户问题*/
    @RequestMapping(value = "/addUserQues")
    @ResponseBody
    @IgnoreAuth
    public Object addUserQues(WxHelp wxHelp) {
        int i = wxHelpService.insertWxHelp(wxHelp);
        if (i > 0) {
            webSocketServer.sendAll("发送消息");
        }
        return writeResultRep();
    }
}
